package com.kyxx.mycommonproject.gouwuche;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kyxx.mycommonproject.MainActivity;
import com.kyxx.mycommonproject.R;
import com.kyxx.mycommonproject.net.ResponseListener;
import com.kyxx.mycommonproject.net.RestClient;
import com.kyxx.mycommonproject.net.UrlUtils;
import com.kyxx.mycommonproject.utils.BigDecimalUtils;
import com.kyxx.mycommonproject.utils.ToastUtils;
import com.kyxx.mycommonproject.utils.UILUtils;
import com.kyxx.mycommonproject.widget.ListViewForScrollView;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2016/8/24.
 */
public class CartFragment extends Fragment implements View.OnClickListener {
    private View layout;
    private TextView tv_edit_cart;

    private RelativeLayout layout_null;
    private ListView listView_cart;
    private RelativeLayout layout_pay_bar;
    private CheckBox btn_check_all;
    private TextView tv_add_all;
    private TextView tv_total;
    private LinearLayout btn_pay;
    private TextView tv_count;
    private TextView tv_price;
    private RelativeLayout layout_edit_bar;
    private CheckBox btn_check_all_deit;
    private Button btn_collect;
    private Button btn_delete;
    private Button btn_more;
    private double allPrice;
    private int allNum;
    private List<CartBean> cartBeanList = new ArrayList<>();
    private CartShopAdapter cartShopAdapter;
    private boolean isEdit; // 是否正在编辑
    private Context context;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (layout != null) {
            initData();
            // 防止多次new出片段对象，造成图片错乱问题
            return layout;
        }
        layout = inflater.inflate(R.layout.fragment_cart, container, false);
        context = getActivity();
        initView();
        setOnListener();
        initData();
        return layout;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onClick(View view) {

    }

    private void initView() {
        tv_edit_cart = (TextView) layout.findViewById(R.id.tv_edit_cart);//编辑按钮
        btn_check_all = (CheckBox) layout.findViewById(R.id.btn_check_all);//选择全部
        tv_price = (TextView) layout.findViewById(R.id.tv_price);//价格
        tv_total = (TextView) layout.findViewById(R.id.tv_total);//总价
        btn_pay = (LinearLayout) layout.findViewById(R.id.btn_pay);//提交订单
        tv_count = (TextView) layout.findViewById(R.id.tv_count);//商品数量
        listView_cart = (ListView) layout.findViewById(R.id.listView_cart);//商品列表
        layout_pay_bar = (RelativeLayout) layout.findViewById(R.id.layout_pay_bar);//商品统计layout
        tv_add_all = (TextView) layout.findViewById(R.id.tv_add_all);//合计
        layout_edit_bar = (RelativeLayout) layout.findViewById(R.id.layout_edit_bar);//编辑layout
        btn_check_all_deit = (CheckBox) layout.findViewById(R.id.btn_check_all_deit);//编辑选择全部
        btn_collect = (Button) layout.findViewById(R.id.btn_collect);//收藏按钮
        btn_delete = (Button) layout.findViewById(R.id.btn_delete);//删除按钮
        layout_null = (RelativeLayout) layout.findViewById(R.id.layout_null);//无商品
        btn_more = (Button) layout.findViewById(R.id.btn_more);//去看看
        tv_edit_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editInCart();
            }
        });
        cartShopAdapter = new CartShopAdapter();
        listView_cart.setAdapter(cartShopAdapter);
    }

    private void setOnListener() {
        btn_check_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn_check_all_deit.setChecked(btn_check_all.isChecked());
                for (int i = 0; i < cartBeanList.size(); i++) {
                    cartBeanList.get(i).setStore_checked(btn_check_all.isChecked());
                }
                for (int i = 0; i < cartBeanList.size(); i++) {
                    for (int j = 0; j < cartBeanList.get(i).getGoods_info().size(); j++) {
                        cartBeanList.get(i).getGoods_info().get(j).setGood_checked(btn_check_all.isChecked());
                    }
                }
                cartShopAdapter.notifyDataSetChanged();
                refreshMoney();
            }
        });
        btn_check_all_deit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn_check_all.setChecked(btn_check_all_deit.isChecked());
                for (int i = 0; i < cartBeanList.size(); i++) {
                    cartBeanList.get(i).setStore_checked(btn_check_all.isChecked());
                }
                for (int i = 0; i < cartBeanList.size(); i++) {
                    for (int j = 0; j < cartBeanList.get(i).getGoods_info().size(); j++) {
                        cartBeanList.get(i).getGoods_info().get(j).setGood_checked(btn_check_all.isChecked());
                    }
                }
                cartShopAdapter.notifyDataSetChanged();
                refreshMoney();
            }
        });
        btn_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                MainActivity activity = (MainActivity) getActivity();
//                activity.activeCategory();
            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (allNum == 0) {
                    ToastUtils.show(context, "您没有选择任何商品");
                    return;
                }
                DelCartGoods();
            }
        });
        btn_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (allNum == 0) {
                    ToastUtils.show(context, "您没有选择任何商品");
                    return;
                }
                submitGoods();
            }
        });
    }

    class CartShopAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return cartBeanList.size();
        }

        @Override
        public Object getItem(int i) {
            return cartBeanList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = View.inflate(context, R.layout.item_shop_cart, null);
                holder.btn_check = (CheckBox) view.findViewById(R.id.btn_check);//选中
                holder.iv_shop = (ImageView) view.findViewById(R.id.iv_shop);//店铺图片
                holder.tv_shop = (TextView) view.findViewById(R.id.tv_shop);//店铺名称
                holder.lv_shop = (ListViewForScrollView) view.findViewById(R.id.lv_shop);//店铺列表
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.tv_shop.setText(cartBeanList.get(i).getStore_name());
            final CartAdapter cartAdapter = new CartAdapter(i);
            holder.lv_shop.setAdapter(cartAdapter);
            holder.btn_check.setOnCheckedChangeListener(null);
            for (int x = 0; x < cartBeanList.get(i).getGoods_info().size(); x++) {
                if (!cartBeanList.get(i).getGoods_info().get(x).isGood_checked()) {
                    cartBeanList.get(i).setStore_checked(false);
                    break;
                }
                cartBeanList.get(i).setStore_checked(true);
            }
            holder.btn_check.setChecked(cartBeanList.get(i).isStore_checked());
            holder.btn_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, final boolean b) {
                    cartBeanList.get(i).setStore_checked(b);
                    for (int x = 0; x < cartBeanList.get(i).getGoods_info().size(); x++) {
                        cartBeanList.get(i).getGoods_info().get(x).setGood_checked(b);
                    }
                    notifyDataSetChanged();
                    refreshMoney();
                }
            });
            return view;
        }

        public class ViewHolder {
            CheckBox btn_check;
            ImageView iv_shop;
            TextView tv_shop;
            ListViewForScrollView lv_shop;
        }
    }

    class CartAdapter extends BaseAdapter {
        int parentId;

        public CartAdapter(int parentId) {
            this.parentId = parentId;
        }

        @Override
        public int getCount() {
            return cartBeanList.get(parentId).getGoods_info().size();
        }

        @Override
        public Object getItem(int i) {
            return cartBeanList.get(parentId).getGoods_info().get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            final ViewHolder0 holder;
            if (view == null) {
                holder = new ViewHolder0();
                view = View.inflate(context, R.layout.item_fragment_cart_list, null);
                holder.btn_check = (CheckBox) view.findViewById(R.id.btn_check);//选中
                holder.btn_cart_add = (Button) view.findViewById(R.id.btn_cart_add);//数量增加
                holder.btn_cart_num_edit = (Button) view.findViewById(R.id.btn_cart_num_edit);//数量
                holder.btn_cart_reduce = (Button) view.findViewById(R.id.btn_cart_reduce);//数量减少
                holder.img_goods = (ImageView) view.findViewById(R.id.img_goods);//商品图片
                holder.tv_goods_name = (TextView) view.findViewById(R.id.tv_goods_name);//商品名称
                holder.tv_goods_price = (TextView) view.findViewById(R.id.tv_goods_price);//商品价格
                view.setTag(holder);
            } else {
                holder = (ViewHolder0) view.getTag();
            }
//            UILUtils.displayImage(context, cartBeanList.get(parentId).getGoods_info().get(i).getGoods_image(), holder.img_goods);
            holder.btn_cart_num_edit.setText(cartBeanList.get(parentId).getGoods_info().get(i).getGoods_num());
            holder.tv_goods_name.setText(cartBeanList.get(parentId).getGoods_info().get(i).getGoods_name());
            holder.tv_goods_price.setText("¥" + cartBeanList.get(parentId).getGoods_info().get(i).getGoods_price());
            holder.btn_check.setOnCheckedChangeListener(null);
            holder.btn_check.setChecked(cartBeanList.get(parentId).getGoods_info().get(i).isGood_checked());
            if (Integer.valueOf(cartBeanList.get(parentId).getGoods_info().get(i).getGoods_num()) == 1) {
                holder.btn_cart_reduce.setEnabled(false);
            }
            holder.btn_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, final boolean b) {
                    cartBeanList.get(parentId).getGoods_info().get(i).setGood_checked(b);
                    for (int x = 0; x < cartBeanList.get(parentId).getGoods_info().size(); x++) {
                        if (!cartBeanList.get(parentId).getGoods_info().get(x).isGood_checked()) {
                            cartBeanList.get(parentId).setStore_checked(false);
                            break;
                        }
                        cartBeanList.get(parentId).setStore_checked(true);
                    }
                    cartShopAdapter.notifyDataSetChanged();
                    refreshMoney();
                }
            });
            holder.btn_cart_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    RequestParams params = new RequestParams();
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("token", "111111");
                        jsonObject.put("cartid", cartBeanList.get(parentId).getGoods_info().get(i).getCart_id());
                        jsonObject.put("type", 1);
                        params.put("json", jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    RestClient.post(UrlUtils.AutoChangeCartGoodsNum, params, context, new ResponseListener(context, true) {
                        @Override
                        public void success(int arg0, Header[] arg1, JSONObject result) {
                            try {
                                int num = result.getJSONObject("data").getInt("goods_num");
                                if (num > 1) {
                                    holder.btn_cart_reduce.setEnabled(true);
                                }
                                holder.btn_cart_num_edit.setText(num + "");
                                cartBeanList.get(parentId).getGoods_info().get(i).setGoods_num(num + "");
                                refreshMoney();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
                }
            });
            holder.btn_cart_reduce.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    RequestParams params = new RequestParams();
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("token", "111111");
                        jsonObject.put("cartid", cartBeanList.get(parentId).getGoods_info().get(i).getCart_id());
                        jsonObject.put("type", 2);
                        params.put("json", jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    RestClient.post(UrlUtils.AutoChangeCartGoodsNum, params, context, new ResponseListener(context, true) {
                        @Override
                        public void success(int arg0, Header[] arg1, JSONObject result) {
                            try {
                                int num = result.getJSONObject("data").getInt("goods_num");
                                if (num == 1) {
                                    holder.btn_cart_reduce.setEnabled(false);
                                }
                                holder.btn_cart_num_edit.setText(num + "");
                                cartBeanList.get(parentId).getGoods_info().get(i).setGoods_num(num + "");
                                refreshMoney();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                }
            });
            holder.btn_cart_num_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    goodNum = Integer.valueOf(cartBeanList.get(parentId).getGoods_info().get(i).getGoods_num());
                    showDialog(cartBeanList.get(parentId).getGoods_info().get(i));
                }
            });
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.putExtra("goodId", cartBeanList.get(parentId).getGoods_info().get(i).getGoods_id());
//                    intent.setClass(getActivity(), GoodDetailActivty.class);
//                    context.startActivity(intent);
                }
            });
            return view;
        }

        class ViewHolder0 {
            CheckBox btn_check;
            Button btn_cart_add;
            Button btn_cart_num_edit;
            Button btn_cart_reduce;
            ImageView img_goods;
            TextView tv_goods_name;
            TextView tv_goods_price;
        }
    }

    private List<CartBean> transformBeanList(List<CartBean.GoodsInfoBean> contentBeanList) {
        List<CartBean> list = new ArrayList<>();
        for (int i = 0; i <= contentBeanList.size(); i++) {
            CartBean.GoodsInfoBean contentBean = contentBeanList.get(0);
            contentBeanList.remove(contentBean);
            CartBean cartBean = new CartBean();
            cartBean.getGoods_info().add(contentBean);
            cartBean.setStore_id(contentBean.getStore_id());
            cartBean.setStore_name(contentBean.getStore_name() + contentBean.getStore_id());
            for (int j = 0; j < contentBeanList.size(); ) {
                CartBean.GoodsInfoBean _contentBean = contentBeanList.get(j);
                if (_contentBean.getStore_id().equals(contentBean.getStore_id())) {
                    cartBean.getGoods_info().add(_contentBean);
                    contentBeanList.remove(_contentBean);
                } else {
                    j++;
                }
            }
            list.add(cartBean);
        }
        return list;
    }

    private void refreshMoney() {
        allPrice = 0;
        allNum = 0;
        int num = 0;
        for (int i = 0; i < cartBeanList.size(); i++) {
            num = cartBeanList.get(i).getGoods_info().size() + num;
            for (int j = 0; j < cartBeanList.get(i).getGoods_info().size(); j++) {
                if (cartBeanList.get(i).getGoods_info().get(j).isGood_checked()) {
                    double p = Double.valueOf(cartBeanList.get(i).getGoods_info().get(j).getGoods_price());
                    int n = Integer.valueOf(cartBeanList.get(i).getGoods_info().get(j).getGoods_num());
                    allPrice = allPrice + BigDecimalUtils.mul(p, n);
                    allNum = allNum + Integer.valueOf(cartBeanList.get(i).getGoods_info().get(j).getGoods_num());
                }
            }
        }

//        MainActivity activity = (MainActivity) getActivity();
//        activity.initInCartNum(num);
        tv_price.setText("¥" + +allPrice);
        tv_total.setText("总额：¥" + allPrice);
        tv_count.setText("(" + allNum + ")");
        int isCheckedAll = 0;
        for (int i = 0; i < cartBeanList.size(); i++) {
            if (cartBeanList.get(i).isStore_checked()) {
                isCheckedAll++;
            }
        }
        if (isCheckedAll == cartBeanList.size()) {
            btn_check_all.setChecked(true);
            btn_check_all_deit.setChecked(true);
        } else {
            btn_check_all.setChecked(false);
            btn_check_all_deit.setChecked(false);
        }
    }

    int goodNum;

    private void showDialog(final CartBean.GoodsInfoBean contentBean) {
        LinearLayout layout = (LinearLayout) getActivity().getLayoutInflater().inflate(
                R.layout.dialog_modify_cart_num, null);
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(layout);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        final Button btn_cart_reduce = (Button) layout.findViewById(R.id.btn_cart_reduce);//数量减少
        final EditText et_cart_num_edit = (EditText) layout.findViewById(R.id.et_cart_num_edit);//数量
        final Button btn_cart_add = (Button) layout.findViewById(R.id.btn_cart_add);//数量增加
        LinearLayout dialog_cancel_ll = (LinearLayout) layout.findViewById(R.id.dialog_cancel_ll);//取消
        LinearLayout dialog_confirm_ll = (LinearLayout) layout.findViewById(R.id.dialog_confirm_ll);//确认
        dialog.show();
        et_cart_num_edit.setText("" + goodNum);
        if (goodNum == 1) {
            btn_cart_reduce.setEnabled(false);
        }
        et_cart_num_edit.setSelection(et_cart_num_edit.getText().length());
        et_cart_num_edit.setFocusable(true);
        et_cart_num_edit.setFocusableInTouchMode(true);
        et_cart_num_edit.requestFocus();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) et_cart_num_edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(et_cart_num_edit, 0);
            }
        }, 500);
        btn_cart_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (goodNum > 1) {
                    goodNum--;
                    et_cart_num_edit.setText(goodNum + "");
                    if (goodNum == 1) {
                        btn_cart_reduce.setEnabled(false);
                    }
                }
            }
        });
        btn_cart_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (goodNum == 1) {
                    btn_cart_reduce.setEnabled(true);
                }
                goodNum++;
                et_cart_num_edit.setText(goodNum + "");
            }
        });
        dialog_cancel_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog_confirm_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = et_cart_num_edit.getText().toString();
                if (!"".equals(s)) {
                    goodNum = Integer.valueOf(s);
                }
                RequestParams params = new RequestParams();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("token", "111111");
                    jsonObject.put("cartid", contentBean.getCart_id());
                    jsonObject.put("num", goodNum);
                    params.put("json", jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RestClient.post(UrlUtils.ResetCartGoodsNum, params, context, new ResponseListener(context, true) {
                    @Override
                    public void success(int arg0, Header[] arg1, JSONObject result) {
                        try {
                            goodNum = result.getJSONObject("data").getInt("goods_num");
                            contentBean.setGoods_num("" + goodNum);
                            refreshMoney();
                            cartShopAdapter.notifyDataSetChanged();
                            dialog.dismiss();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

            }
        });

    }

    /**
     * 编辑
     */
    private void editInCart() {
        isEdit = !isEdit;
        if (isEdit) {
            tv_edit_cart.setText("完成");
            layout_pay_bar.setVisibility(View.GONE);
            layout_edit_bar.setVisibility(View.VISIBLE);
        } else {
            tv_edit_cart.setText("编辑");
            layout_pay_bar.setVisibility(View.VISIBLE);
            layout_edit_bar.setVisibility(View.GONE);
        }
    }

    private void initData() {
        RequestParams params = new RequestParams();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("token", "111111");
            params.put("json", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RestClient.post(UrlUtils.CART_MA, params, context, new ResponseListener(context, true) {
            @Override
            public void success(int arg0, Header[] arg1, JSONObject result) {
                String s = null;
                try {
                    s = result.getJSONArray("data").toString();
                    cartBeanList = com.alibaba.fastjson.JSONObject.parseArray(s, CartBean.class);
                    refreshMoney();
                    if (cartBeanList.size() == 0) {
                        layout_null.setVisibility(View.VISIBLE);
                        layout_pay_bar.setVisibility(View.GONE);
                        layout_edit_bar.setVisibility(View.GONE);
                        tv_edit_cart.setVisibility(View.GONE);
                    } else {
                        tv_edit_cart.setVisibility(View.VISIBLE);
                        if (isEdit) {
                            tv_edit_cart.setText("完成");
                            layout_pay_bar.setVisibility(View.GONE);
                            layout_edit_bar.setVisibility(View.VISIBLE);
                        } else {
                            tv_edit_cart.setText("编辑");
                            layout_pay_bar.setVisibility(View.VISIBLE);
                            layout_edit_bar.setVisibility(View.GONE);
                        }
                        layout_null.setVisibility(View.GONE);
//                        cartShopAdapter = new CartShopAdapter();
//                        listView_cart.setAdapter(cartShopAdapter);
                        cartShopAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /*
     自动加减商品数量
     */
    private void autoChangeCartGoodsNum(String id, int type) {
        RequestParams params = new RequestParams();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("token", "11111");
            jsonObject.put("cartid", id);
            params.put("json", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RestClient.post(UrlUtils.AutoChangeCartGoodsNum, params, context, new ResponseListener(context, true) {
            @Override
            public void success(int arg0, Header[] arg1, JSONObject result) {

            }
        });
    }

    private void DelCartGoods() {
        RequestParams params = new RequestParams();
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < cartBeanList.size(); i++) {
            for (int j = 0; j < cartBeanList.get(i).getGoods_info().size(); j++) {
                if (cartBeanList.get(i).getGoods_info().get(j).isGood_checked()) {
                    jsonArray.put(cartBeanList.get(i).getGoods_info().get(j).getCart_id());
                }
            }
        }
        try {
            jsonObject.put("token", "111111");
            jsonObject.put("cartids", jsonArray);
            params.put("json", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RestClient.post(UrlUtils.DelCartGoods, params, context, new ResponseListener(context, true) {
            @Override
            public void success(int arg0, Header[] arg1, JSONObject result) {
                initData();
            }
        });
    }

    private void submitGoods() {
        List<CartBean.GoodsInfoBean> goodsInfoBeanList = new ArrayList<>();
        for (int i = 0; i < cartBeanList.size(); i++) {
            for (int j = 0; j < cartBeanList.get(i).getGoods_info().size(); j++) {
                if (cartBeanList.get(i).getGoods_info().get(j).isGood_checked()) {
                    goodsInfoBeanList.add(cartBeanList.get(i).getGoods_info().get(j));
                }
            }
        }
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        intent.putExtra("submitgoods", (Serializable) goodsInfoBeanList);
//        intent.setClass(getActivity(), SubmitOrderActivity.class);
//        getActivity().startActivity(intent);

    }
}
